﻿using System;
using System.IO;
using System.Threading.Tasks;

namespace HelperLibrary
{
    /// <summary>
    /// 日志工具类
    /// </summary>
    public class LogHelper
    {
        /// <summary>
        /// 基础路径
        /// </summary>
        private static string basePath = Directory.GetCurrentDirectory();
        /// <summary>
        /// 异常日志子路径
        /// </summary>
        private static string ePath = "/log_exception/";
        /// <summary>
        /// 异常日志访问锁
        /// </summary>
        private static object eLock = new object();
        /// <summary>
        /// 调试日志子路径
        /// </summary>
        private static string dPath = "/log_debug/";
        /// <summary>
        /// 调试日志访问锁
        /// </summary>
        private static object dLock = new object();

        /// <summary>
        /// 记录异常日志
        /// </summary>
        /// <param name="ex"></param>
        public static void LogErr(Exception ex)
        {
            lock (eLock)
            {
                try
                {
                    Task.Run(() =>
                    {
                        // 创建异常日志文件夹
                        CreateEDirectory();
                        string logFilePath = $"{basePath}{ePath}{DateTime.Now.ToYMD()}_err.log";
                        using (StreamWriter sw = new StreamWriter(logFilePath, append: true))
                        {
                            sw.AutoFlush = true;
                            string time = DateTime.Now.ToYMDHMS();
                            sw.WriteLine($"[{time}]:{ex.Message}");
                            if (ex.InnerException != null)
                            {
                                sw.WriteLine(ex.InnerException.Message);
                                sw.WriteLine(ex.InnerException.StackTrace);
                            }
                            else
                            {
                                sw.WriteLine(ex.StackTrace);
                            }
                            sw.Close();
                        }
                    });
                }
                catch { }
            }
        }

        /// <summary>
        /// 创建异常日志目录
        /// </summary>
        private static void CreateEDirectory()
        {
            string exceptionPath = $@"{basePath}{ePath}";
            if (!Directory.Exists(exceptionPath))
            {
                Directory.CreateDirectory(exceptionPath);
            }
        }

        /// <summary>
        /// 记录调试日志
        /// </summary>
        /// <param name="ex"></param>
        public static void LogDebug(string msg)
        {
            lock (dLock)
            {
                try
                {
                    Task.Run(() =>
                    {
                        // 创建调试日志文件夹
                        CreateDDirectory();
                        string logFilePath = $"{basePath}{dPath}{DateTime.Now.ToYMD()}_debug.log";
                        using (StreamWriter sw = new StreamWriter(logFilePath, append: true))
                        {
                            sw.AutoFlush = true;
                            string time = DateTime.Now.ToYMDHMS();
                            sw.WriteLine($"[{time}]:{msg}");
                            sw.Close();
                        }
                    });
                }
                catch { }
            }
        }

        /// <summary>
        /// 创建调试日志目录
        /// </summary>
        private static void CreateDDirectory()
        {
            string debugPath = $@"{basePath}{dPath}";
            if (!Directory.Exists(debugPath))
            {
                Directory.CreateDirectory(debugPath);
            }
        }
    }
}
